// ==UserScript==
// @name         Platform Spoofer
// @namespace    https://viayoo.com/
// @version      0.2
// @description  modtify platform Only.
// @author       Via
// @match        *://*/*
// @license      MIT
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';
    const FAKE_PLATFORM = 'Mac';
    const spoofNavigator = new Proxy(navigator, {
        get(target, prop) {
            if (prop === 'platform') {
                return FAKE_PLATFORM;
            }
            return typeof target[prop] === 'function' ?
                target[prop].bind(target) :
                target[prop];
        },

        getOwnPropertyDescriptor(target, prop) {
            if (prop === 'platform') {
                return {
                    value: FAKE_PLATFORM,
                    writable: false,
                    configurable: true,
                    enumerable: true
                };
            }
            return Object.getOwnPropertyDescriptor(target, prop);
        }
    });

    try {
        if (Object.getOwnPropertyDescriptor(navigator, 'platform')) {
            navigator.__defineGetter__('platform', function() {
                return FAKE_PLATFORM;
            });
        } else {
            Object.defineProperty(navigator, 'platform', {
                get: function() {
                    return FAKE_PLATFORM;
                },
                configurable: true,
                enumerable: true
            });
        }
        Object.defineProperty(window, 'navigator', {
            value: spoofNavigator,
            writable: false,
            configurable: true
        });
        Object.defineProperty(Navigator.prototype, 'platform', {
            get: function() {
                return FAKE_PLATFORM;
            },
            configurable: true,
            enumerable: true
        });
        const originalIndexOf = String.prototype.indexOf;
        String.prototype.indexOf = function(searchString) {
            if (this === FAKE_PLATFORM) {
                if (searchString === 'Win' || searchString === 'Linux' || searchString === 'X11') {
                    return -1;
                }
                if (searchString === 'Mac') {
                    return 0;
                }
            }
            return originalIndexOf.apply(this, arguments);
        };

    } catch (e) {
        console.warn('Platform spoofing fallback:', e);
        window.navigator = spoofNavigator;
    }
    Object.freeze(window.navigator);

    // 确保在DOM加载前生效
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', function() {
            Object.defineProperty(window, 'navigator', {
                value: spoofNavigator,
                writable: false
            });
        });
    }
})();